package com.yami.shop.personnft.common.listener;

import com.yami.shop.bean.model.NftTransferRecord;
import com.yami.shop.common.bean.NftMnemonic;
import com.yami.shop.common.config.RocketMqConstant;
import com.yami.shop.common.util.Json;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.personnft.common.service.NftTransferRecordService;
import com.yami.shop.personnft.common.service.PersonNftService;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.sys.common.service.ShopEmployeeService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author 下单资产划转
 * @date 2021/1/7
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.NFT_NOTIFY_SHIFT,consumerGroup = RocketMqConstant.NFT_NOTIFY_SHIFT)
@Slf4j
public class NftNotifyConsumer implements RocketMQListener<List<Map<String,String>> > {


    @Autowired
    private ShopEmployeeService shopEmployeeService;
    @Autowired
    private PersonNftService personNftService;
    @Autowired
    private RocketMQTemplate stockMqTemplate;
    @Autowired
    private NftTransferRecordService nftTransferRecordService;

    @Autowired
    private ShopDetailService shopDetailService;

    @Autowired
    private ShopConfig shopConfig;
//    @Autowired
//    private NftDenomService nftDenomService;

    private static final Logger LOG = LoggerFactory.getLogger(NftNotifyConsumer.class);

    @SneakyThrows
    @Override
    public  void onMessage(List<Map<String, String>> mapList) {
//        log.info("订单空投拆盲盒合成下nft资产转移... message: " + Json.toJsonString(mapList));
//        //获取助记词开始
//        String denomId = mapList.get(0).get("denomId");
//        if(StringUtils.isEmpty(denomId)){
//            log.info("denomId为空");
//            return;
//        }
//        NftDenom tenantIdByDenomId = nftDenomService.getTenantIdByDenomId(denomId);
//        if(tenantIdByDenomId==null||tenantIdByDenomId.getTenantid()==null){
//            log.info("获取铸币者信息失败");
//            return;
//        }
//        String mnemonic = "";
//        String address = "";
//        NftMnemonic nftMnemonic = shopConfig.getNftMnemonic();  // 获取配置助计词
//        if (nftMnemonic != null) {
//            mnemonic = nftMnemonic.getMnemonic();
//            address = nftMnemonic.getDtcAddress();
//        }else{
//            log.info("获取铸币者助记词失败");
//            return;
//        }
//        List<NftTransferRecord> list = new ArrayList<>();
//        //获取助记词结束
//        for(int i=0;i<mapList.size();i++){
//            if(ObjectUtils.isEmpty(mapList.get(i).get("reci"))||ObjectUtils.isEmpty(denomId) ||ObjectUtils.isEmpty(mapList.get(i).get("nftId"))){
//                LOG.info("nft资产转移参数不足... message:reci: "+mapList.get(i).get("reci") +"denomId :"+ObjectUtils.isEmpty(denomId)
//                        +"nftId:"+mapList.get(i).get("nftId"));
//                continue;
//            }
//            String userId = mapList.get(i).get("userId");
//            String nftId = mapList.get(i).get("nftId");
//            String reci = mapList.get(i).get("reci");
//
//            NftTransferRecord record = new NftTransferRecord();
//            record.setDenomId(denomId);
//            record.setNftId(nftId);
//            record.setReci(reci);
//            record.setUserId(userId);
//            record.setMnemonic(mnemonic);
//            record.setSenderAddress(address);
//            record.setType(4);
//            list.add(record);
//        }
//        nftTransferRecordService.saveBatch(list);
//        log.info("NFT购买划转 已保存到待划转【Mnemonic:" + nftMnemonic.getMnemonic() + "denomId:" + denomId + "转移给" + mapList.get(0).get("userId") + "数量:" + list.size() + "个】");
    }


}
